package org.example.exception;

import org.example.entity.AjaxResult;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.nio.file.AccessDeniedException;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionAdvice
{

    @ExceptionHandler(value = LoginException.class)
    public AjaxResult handleLoginException(LoginException ex){
        return AjaxResult.error(401, ex.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public AjaxResult handleLoginException(Exception ex){
        return AjaxResult.error( ex.getMessage());
    }

//    @ExceptionHandler(value = NoPremsException.class)
//    public AjaxResult handleLoginException(NoPremsException ex){
//        return AjaxResult.error( 403,ex.getMessage());
//    }

    @ExceptionHandler(value = BadCredentialsException.class)
    public AjaxResult BadCredentialsException(BadCredentialsException ex){
        return AjaxResult.error("用户名或密码不正确");
    }

    @ExceptionHandler(value = AccessDeniedException.class)
    public AjaxResult AccessDeniedException(AccessDeniedException ex){
        return AjaxResult.error("无权访问");
    }

}
